package com.example.stag;

import sun.nio.cs.UTF_8;

import javax.print.DocFlavor;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/commodityBatchLookup")
public class CommodityBatchLookupServlet extends HttpServlet {

    @Override
    public void init() {
        UserSQLOperation.connect();
        CommoditySQLOperation.connect();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        UserSQLOperation.connect();
        CommoditySQLOperation.connect();
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        try {
            String batch = req.getParameter("batch");
            ResultSet resultSet = CommoditySQLOperation.searchByBatch(batch);
            PrintWriter writer = resp.getWriter();
            if (!resultSet.next()) {
                writer.println("wrong");
            } else {
                writer.println("OK");
                String s11 = URLDecoder.decode(resultSet.getString("Description"), "utf-8");
                writer.println(s11);
                writer.println(resultSet.getString("SaleStatus"));
                writer.println(resultSet.getString("OpenStatus"));
                writer.println(resultSet.getString("null"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
